package com.financial.business.integral.config;

import com.alibaba.fastjson.JSON;
import com.financial.business.integral.dto.FinancialSecurityConfDTO;
import com.financial.business.integral.utils.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author WANGNING
 * @Email wyn20140226@aliyun.com
 * @Description //优店平台获取token
 * @Date 2019/12/6 10:25
 */
@Component
public class YDAccessTokenConfig {
    public static final Logger logger = LoggerFactory.getLogger(YDAccessTokenConfig.class);

    @Autowired
    private HttpClientUtils httpClientUtils;

    @Autowired
    private RedisUtils redisUtils;

    /*获取token*/
    public String getYDAccessToken(String platformName, String passWord, String secret, String sign) {
        logger.info("YDAccessTokenConfig.getYDAccessToken()-----获取优店积分中心的参数信息:platformName：" + platformName + "----passWord:" + secret + "---sign:" + sign);
        String result = null;
        try {
            FinancialSecurityConfDTO financialSecurityConfDTO = new FinancialSecurityConfDTO();
            financialSecurityConfDTO.setPlatformName(platformName);
            financialSecurityConfDTO.setPassWord(passWord);
            financialSecurityConfDTO.setSecret(secret);
            financialSecurityConfDTO.setSign(sign);
            result = httpClientUtils.doPost(UrlConfig.INTEGRAL_YD_ACCESSTOKEN_URL, MapUtils.objectToMap(financialSecurityConfDTO));
            Map<String, Object> map = (Map<String, Object>) JSON.parseObject(result, Map.class);
            String accessToken = (String) map.get("accessToken");
            int time = (int) map.get("time");
            /*保存到redis数据库*/
            redisUtils.setKey("custumerAccessToken", accessToken, time - 10);
            logger.info("YDAccessTokenConfig.getYDAccessToken()-----获取优店积分中心的accessToken:" + accessToken);
            return accessToken;
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("YDAccessTokenConfig.getYDAccessToken()-----获取优店积分中心的accessToken失败:" + e.getMessage());
        }
        return result;
    }

    /*获取退款token*/
    public String getYDRefundAccessToken(String platformName, String passWord, String secret, String platfromNo) {
        logger.info("YDAccessTokenConfig.getYDRefundAccessToken()-----获取优店积分中心的参数信息:platformName：" + platformName + "----passWord:" + passWord);
        String result = null;
        String accessToken = null;
        try {
            String sign= MD5Utils.md5(platformName + platfromNo + passWord + secret);
            Map<String, Object> map = new HashMap<>();
            map.put("platfrom_no", platfromNo);
            map.put("user_name", platformName);
            map.put("pass_word", passWord);
            map.put("secret_key", secret);
            map.put("sign", sign);
            /*将map转换为json*/
            String json = JsonUtils.objectToJson(map);
            result = httpClientUtils.doPostJson(UrlConfig.INTEGRAL_YD_ACCESSTOKEN_URL, json);
            Map<String, Object> resultMap = (Map<String, Object>) JSON.parseObject(result, Map.class);
            String resultJson = resultMap.get("result").toString();
            Map<String, Object> toMap = JsonUtils.jsonToMap(resultJson);
            if (null != toMap && toMap.size() > 1 && toMap.get("tgAccessToken") != null) {
                accessToken = toMap.get("financialYDRefundAccessToken").toString();
                int time = (int) toMap.get("time");
                redisUtils.setKey("tgAccessToken", accessToken, time - 1);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return accessToken;
    }
}
